package de.hpi.generator.helpers;

import de.hpi.generator.generator.storages.UniqueValuesDataStorage;
import de.hpi.generator.generator.storages.ValueSeriaInfo;
import de.hpi.generator.generator.storages.ValuesDataStorage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class FileSpliter {

    protected static int MAXSUPPORTEDLINESNUMBER = 100000;

    public static String[][] splitTableFileToMemory(String aTablePath) throws Exception {
        String aTableText = FileHelper.readTextFromFile(aTablePath);

        String[] aTableLines = aTableText.split("\n");
        if (aTableLines.length > MAXSUPPORTEDLINESNUMBER) {
            throw new Exception(String.format("The table %s is to large for validation", aTablePath));
        }

        return splitLinesToValues(aTableLines);
    }

    protected static String[][] splitLinesToValues(String[] aTableLines) {

        int numberOfColumns = aTableLines[0].split(",").length;

        String[][] result = new String[numberOfColumns][aTableLines.length];

        for (int aRowNumber = 0; aRowNumber < aTableLines.length; aRowNumber++) {
            String[] aSplitedRow = aTableLines[aRowNumber].split(",");            // "1","2","3"

            for (int aColumnNr = 0; aColumnNr < aSplitedRow.length; aColumnNr++) {
                result[aColumnNr][aRowNumber] = aSplitedRow[aColumnNr];

            }
        }
        return result;
    }



    public static void splitTableToColumnsOnDisk(String aTableFilePath) throws Exception {

        String aOutputPath = aTableFilePath.replace(".csv", "");
        FileHelper.recreateDirectories(aOutputPath);

        FileReader aFileReader = null;
        BufferedReader aBufferedReader = null;
        aFileReader = new FileReader(new File(aTableFilePath));
        aBufferedReader = new BufferedReader(aFileReader);
        String aTableRow = null;

        String[] aColumnNamesArray = aBufferedReader.readLine().split(",");
        ArrayList<ValuesDataStorage> columnValuesDataStorages = openColumnsFiles(aOutputPath, aColumnNamesArray);

        while ((aTableRow = aBufferedReader.readLine()) != null) {
            String[] aCells = aTableRow.split(",");
            for (int aLineIndex = 0; aLineIndex < aCells.length; aLineIndex++) {
                int aValue = Integer.parseInt(aCells[aLineIndex]);
                ValueSeriaInfo aValueSeriaInfo = new ValueSeriaInfo(aValue, -1);
                columnValuesDataStorages.get(aLineIndex).appendValue(aValueSeriaInfo);
            }
        }

        closeColumnsFiles(columnValuesDataStorages);
        aBufferedReader.close();
    }

    protected static ArrayList<ValuesDataStorage> openColumnsFiles(String aOutputPath, String[] aColumnNamesArray) throws Exception {
        ArrayList<ValuesDataStorage> aValuesDataStorages = new ArrayList<ValuesDataStorage>();

        for (String aColumnName : aColumnNamesArray) {
            String aColumnOutputPath = aOutputPath + "\\" + aColumnName + ".bin";
            FileHelper.recreateFile(aColumnOutputPath);

            ValuesDataStorage aValuesDataStorage = new ValuesDataStorage(aColumnOutputPath);
            aValuesDataStorage.openForAppend();
            aValuesDataStorages.add(aValuesDataStorage);
        }

        return aValuesDataStorages;


    }

    protected static void closeColumnsFiles(ArrayList<ValuesDataStorage> columnValuesDataStorages) throws Exception {
        for (ValuesDataStorage aColumnValuesDataStorage : columnValuesDataStorages) {
            aColumnValuesDataStorage.closeAfterAppend();
        }
    }
}
